/* M.Nielsen April 2008 mniel@cbs.dtu.dk */

/* 
Copyright (C) 2008-2015 Danish Technical University

This suite of programs and library routine is free software. You can 
redistribute it and/or modify it under the terms of the GNU General Public 
License as published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.

In other words, you are free to modify, copy, or redistribute this
source code and its documentation in any way you like, but you must
distribute all derivative versions as free software under the same
terms that I've provided my code to you (i.e. the GNU General Public
License). This precludes any use of the code in proprietary or
commercial software unless your source code is made freely available.

If you wish to use the code under a different Open Source license
that's not compatible with the GPL (like the Artistic License, BSD
license, or the Netscape Public License), please contact me
(Morten Nielsen, mniel@cbs.dtu.dk) for permission.

Incorporation into commercial software under non-GPL terms is possible
by obtaining a specially licensed version from The Danish Technical University.
Contact Morten Nielsen (mniel@cbs.dtu.dk) to arrange licensing terms.

This software is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
*/


#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "utils.h"

FILENAME p_blm;

PARAM	param[] = {
	"-p", VFNAME p_blm, "prefix", "",
	0
};

void generate_indices(int *vec, int length, int partitions)

{
	int	i;
	int j;
	int	w;
	int	t;
	float k;

	if (partitions > length || partitions < 2){
		printf( "Error: Non-sensical number of partitions\n");
		exit( 1 );
	}
	// Initialize vector, seeting "fraction" of the entires to values from 0 to (number of CV partitions - 1)
	i = 0;
	k = (1.0/partitions)*length;

	for (j = 1; j <= (partitions-1); j++){
		for (i; i < (int)(k*j); i++){
			vec[i] = j;
		}
	}
	
	srand(time(NULL));
	// Randomly shuffle
	for (i = length-1; i > 0; i--){
		w = rand()%(length-1);
		t = vec[i];
		vec[i] = vec[w];
		vec[w] = t;
	}
	
}

void	print_data( int *vec, char *filename2, char *filename3, PEPLIST *peplist )

{
	PEPLIST *pl;
	FILE	*fp_train;
	FILE	*fp_test;
	int	l = 0;

	if ( ( fp_train = fopen( filename2, "w" ) ) == NULL ) {
		printf( "Cannot open file %s\n", filename2 );
		exit( 1 );
	}
	if ( ( fp_test = fopen( filename3, "w" ) ) == NULL ) {
		printf( "Cannot open file %s\n", filename3 );
		exit( 1 );
	}
	for ( pl=peplist; pl; pl=pl->next ) {
		if (vec[l] == 1){
			fprintf( fp_test, "%s %f\n", pl->pep, pl->score);
		} else {
			fprintf( fp_train, "%s %f\n", pl->pep, pl->score);
		}
		l++;
	}
	
	fclose( fp_train );
	fclose( fp_test );
}


main (int argc, char *argv[]){
	PEPLIST		*peplist, *pl;
	int *vec;
	int i;
	float	**inp;
	int	n;
	char test[50];
	char train[50];
	pparse( &argc, &argv, param, 1, "peptidefile");
	sprintf(test, "test_%s.pep", p_blm);
	sprintf(train, "train_%s.pep", p_blm);
	set_pep_verbose(0);
	//inp = init_input( file, &nc, &n );
	peplist = peplist_read( argv[1] );
	for (n = 0, pl=peplist; pl; pl=pl->next, n++){}
	vec = ivector( 0, n-1 ); /* Allocate vector for input */
	generate_indices(vec, n, 5); //vector, number of entries, number of folds
	print_data(vec, train, test, peplist);
	exit(0);
}